<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>首页</title>
    <script src="./cookies.js"></script>
  </head>
  <body>
    <h3>
      欢迎回来：<span id="spUsername">昵称</span>
      <button id="btnLogout">登出</button>
    </h3>

    <script>
      // 判断有无logined=true这样一个cookie
      const logined = getCookie("logined")

      // 有则进一步读取username这个cookie的值 丢给spUsername显示
      if(logined){
        spUsername.innerText = getCookie("username")
      }

      // 没有则一脚踹到登录页
      else{
        // window.location.href = "./login.html"
        window.location.assign("./login.html")
      }

      btnLogout.onclick = function(e){
        // 清除username
        deleteCookie("username")

        // 清除logined
        deleteCookie("logined")

        // 帮用户跳转登录页
        window.location.assign("./login.html")
      }
    </script>
  </body>
</html>
